package azalea.mqtt.message.builder.impl;

import azalea.mqtt.message.utils.MsgUtils;

public class PublishDataImpl {
	/*
	 * public static byte[] publish(boolean dup,int qos,boolean retain,int
	 * identifier,byte[] payload,int payloadLen) { return publish( dup, qos,
	 * retain,(byte)((identifier>>8)&0xff), (byte)(identifier&0xff), payload,
	 * payloadLen); }
	 */
	public static byte[] publish(boolean dup,int qos,boolean retain,byte msb,byte lsb,byte[] payload,int payloadLen) {
		int head=((retain?1:0)|(qos<<1&0x00000110))|(dup?1:0)<<3;
		if(qos==0) {
			return MsgUtils.commMsg(3,head, payload, payloadLen);
		}else {
			return MsgUtils.commMsg(3,head, msb,lsb, payload, payloadLen);
		}
	}
	public static byte[] puback(byte msb,byte lsb) {
		// 0x40 0x42
		return new byte[]{(byte)0b01000000,(byte)0b01000010,msb,lsb};
	}
	public static byte[] pubrec(byte msb,byte lsb) {
		return new byte[]{(byte)0b01010000,(byte)0b00000010,msb,lsb};
	}
	/**
	 * Publish release
	 * @param msb
	 * @param lsb
	 * @return
	 */
	public static byte[] pubrel(byte msb,byte lsb) {
		return new byte[]{(byte)0b01100010,(byte)0b0000010,msb,lsb};
	}
	public static byte[] pubcomp(byte msb,byte lsb) {
		/**
		 * Figure 3.18 – PUBCOMP Packet fixed header
|Bit        |7      |6      |5      |4      |3      |2      |1      |0
|byte 1     |MQTT Control Packet type (7)   |Reserved
|           |0      |1      |1      |1      |0      |0      |0      |0
|byte 2     |Remaining Length (2)
|           |0      |0      |0      |0      |0      |0      |1      |0
		 */
		return new byte[]{(byte)0b01110000,(byte)0b0000010,msb,lsb};
	}
}
